﻿using My.Admin.Dto.System;
using My.Admin.Model.System;
using My.Admin.Service.System.IService;

namespace My.Admin.Service.System
{
	public class SysRoleMenuService : BaseService<SysRoleMenu>, ISysRoleMenuService
	{
		/// <summary>
		/// 根据角色Id集合获取菜单Id集合
		/// </summary>
		/// <param name="roleIdList"></param>
		/// <returns></returns>
		public async Task<List<long>> GetRoleMenuIdList(List<long> roleIdList)
		{
			return await AsQueryable()
				.Where(u => roleIdList.Contains(u.RoleId))
				.Select(u => u.MenuId).ToListAsync();
		}

		/// <summary>
		/// 授权角色菜单
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task GrantRoleMenu(RoleMenuInput input)
		{
			await DeleteAsync(u => u.RoleId == input.Id);
			var menus = input.MenuIdList.Select(u => new SysRoleMenu
			{
				RoleId = input.Id,
				MenuId = u
			}).ToList();

			await InsertRangeAsync(menus);
		}

		/// <summary>
		/// 根据角色Id删除角色菜单
		/// </summary>
		/// <param name="roleId"></param>
		/// <returns></returns>
		public async Task DeleteRoleMenuByRoleId(long roleId)
		{
			await DeleteAsync(u => u.RoleId == roleId);
		}

		/// <summary>
		/// 根据菜单Id集合删除角色菜单
		/// </summary>
		/// <param name="menuIdList"></param>
		/// <returns></returns>
		public async Task DeleteRoleMenuByMenuIdList(List<long> menuIdList)
		{
			await DeleteAsync(u => menuIdList.Contains(u.MenuId));
		}
	}
}
